<template>
  <div>
    <el-dialog
      :title="info.type == 'add' ? '新增' : '修改'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="上级分类" prop="pid">
          <el-select v-model="ruleForm.pid">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="(item, index) in selectList"
              :key="index + 'option'"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" prop="catename">
          <el-input v-model="ruleForm.catename"></el-input>
        </el-form-item>
        <el-form-item label="分类名称" prop="catename">
          <!-- action 上传地址 -->
          <!-- limit 个数的限制 -->
          <!-- on-exceed 文件超出时的钩子 -->
          <!-- on-change 修改文件时的钩子 -->
          <!-- file-list 文件的存放位置 -->
          <!-- auto-upload 是否自动上传 -->
          <el-upload
            action=""
            list-type="picture-card"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
            :auto-upload="false"
            :on-change="handleChange"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="info.isShow = false">取 消</el-button>
        <el-button v-if="info.type == 'add'" type="primary" @click="submitForm"
          >新增</el-button
        >
        <el-button v-else type="primary" @click="submitForm">修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateList, cateAdd, menuEdit } from "../../http";
export default {
  props: ["info"],
  data() {
    return {
      // 上传的图片
      fileList: [],
      // 下拉菜单的数据
      selectList: [],
      ruleForm: {},
      rules: {
        title: [{ required: true, message: "请输入菜单标题", trigger: "blur" }],
      },
    };
  },
  created() {
    // 获取下拉菜单的数据
    this.getSelectList();
  },
  methods: {
    handleChange(file, fileList) {
      this.ruleForm.img = file.raw;
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    async getSelectList() {
      let res = await getCateList();
      this.selectList = res.list;
    },
    setValue(val) {
      // 根据父组件传来的值（新增？修改？）给表单填入数据
      this.ruleForm = { ...val };
    },
    // 关闭弹窗
    // handleClose(done) {
    //   done();
    // },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 通过校验 准备提交数据
          console.log(this.ruleForm);
          // 只要有图片 就必须有fd格式提交
          var fd = new FormData();
          // for in 遍历对象 将ruleForm里面的数据全部添加到fd中
          // fd是真正提交的数据 ruleForm只是备胎
          for (const key in this.ruleForm) {
            fd.append(key, this.ruleForm[key])
          }
          // console.log(fd); 直接打印fd 看不出来 需要遍历
          // fd.forEach((val, key)=>{
          //   console.log(key, val);
          // })
          let res;
          if (this.info.type === "add") {
            res = await cateAdd(fd);
          } else if (this.info.type === "edit") {
            res = await menuEdit(this.ruleForm);
          }
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: res.msg,
            });
            this.info.isShow = false;
            // 子传父 刷新list组件
            this.$emit("update");
          } else {
            console.log("error submit!!");
            return false;
          }
        }
      });
    },
  },
};
</script>

<style>
</style>